﻿using System;
using System.Collections.Generic;
 
using System.Text;
using GGSharp.Structures;

namespace GGSharp.Packets
{
    public class PubDirResponse50Packet : HeaderPacket
    {
        private byte _type = 0x02;
        private int _seq = (int)DateTime.Now.Ticks;
        private List<PubDirContactBase> _response = new List<PubDirContactBase>();

        public PubDirResponse50Packet()
        {
            this.Type = PacketType.GG_PUBDIR50_REPLY;
        }

        public List<PubDirContactBase> Response
        {
            get { return _response; }
        }

        public PubDirType ResponseType
        {
            get { return (PubDirType)_type; }
        }

        public override void Write(System.IO.BinaryWriter writer)
        {
            throw new NotImplementedException();
        }

        public override void Read(System.IO.BinaryReader reader)
        {
            base.Read(reader);

            _type = reader.ReadByte();
            _seq = reader.ReadInt32();

            this.ReadedLength = 5;

            if (this.ReadedLength < this.Length)
            {
                byte[] bytes = reader.ReadBytes(this.Length - 5);
                int readedResponse = 0;

                while (readedResponse < bytes.Length)
                {
                    PubDirContactBase response;
                    if(ResponseType == PubDirType.SearchReply)
                        response = new PubDirContactSearch();
                    else
                        response = new PubDirPersonal();

                    response.Read(bytes, readedResponse);
                    _response.Add(response);
                    readedResponse += (int)response.ReadedLength;

                    if (readedResponse < bytes.Length && bytes[readedResponse] == '\0')
                        readedResponse++;
                }

                this.ReadedLength += bytes.Length;
            }
        }
    }
}
